package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.config.AppConfig;
import com.example.forum.model.Board;
import com.example.forum.model.User;
import com.example.forum.services.BoardService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/board")
public class BoardController {

    @Resource
    private BoardService boardService;

    /**
     * 创建板块,简而言之就是在板块表中插入一条数据，板块名以及板块的优先级应该是不能重复的！
     * 这一共功能只能有管理员来完成，按理来说还需要进行身份验证！
     * @param name
     * @param sort
     * @return
     */
    @PostMapping("/create")
    public AppResult create(String name,Integer sort){
        Board board = new Board();
        board.setName(name);
        board.setSort(sort);
        int row = boardService.create(board);
        if(row!=1){
            return AppResult.failed(ResultCode.FAILED_CREATE);
        }
        return AppResult.success();
    }

    /**
     * 获取板指定块信息（有必要吗？？？？）
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/getById")
    public AppResult<Board> getBoardInfo(Long id, HttpServletRequest request){
        HttpSession session = request.getSession(false);
        if(session==null){
            return AppResult.failed(ResultCode.FAILED_UNAUTHORIZED);
        }

        if(id==null&&id!=0){
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        Board board = boardService.selectById(id);
        return AppResult.success(board);
    }

    /**
     * 两种思路实现首页导航栏
     * 在⾸⻚显⽰的版块信息，可以通过以下两种⽅式解决
     * ⽅法⼀：单独提供查询前N条记录的接⼝，⽤来控制⾸⻚中版块的个数
     * ⽅法⼆：通过配置表中state字段来实现，提供查询所有版块的接⼝
     * 两种⽅式任选⼀个都可以，项⽬中使⽤⽅法二
     */

    /**
     * 构建首页板块导航栏
     * @param request
     * @return
     */
    @GetMapping("/topList")
    public AppResult<List<Board>> topList(HttpServletRequest request){
        //验证是否登录
        HttpSession session = request.getSession(false);
        if(session==null){
            return AppResult.failed(ResultCode.FAILED_UNAUTHORIZED);
        }
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);

        List<Board> boardList = boardService.selectAllNormal();
        return AppResult.success(boardList);
    }

    /**
     * 构建首页板块导航栏
     * @return
     */
    @GetMapping("/allNormal")
    public AppResult<List<Board>> allNormal (HttpServletRequest request){
        HttpSession session = request.getSession(false);
        if(session==null){
            return AppResult.failed(ResultCode.FAILED_UNAUTHORIZED);
        }
        List<Board> boardList = boardService.selectAllNormal();
        return AppResult.success(boardList);
    }






}
